package main

import (
	"chat/controllers"
	"chat/utils/chat"
	"chat/utils/config"
	"fmt"
	"log"
	"net/http"
)

func main() {
	if err := config.InitConfigYaml("./config/config.yaml"); err != nil {
		log.Println(err)
	}
	server := config.GetGlobalConfig().App.Server
	addr := server.Host + ":" + server.Port

	hub := chat.NewHub()
	go hub.Run()
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("welcome index."))
	})
	http.HandleFunc("/gettoken", controllers.GetToken)

	http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
		chat.ServeWs(hub, w, r)
	})
	fmt.Println("Listen server" + addr)
	if err := http.ListenAndServe(addr, nil); err != nil {
		log.Fatal(err)
	}
}
